package com.siqi.hr.spring.config;

import com.siqi.hr.common.redis.cache.SystemCache;
import com.siqi.hr.system.common.authority.Sysdata;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationFactory;

public class AppConfiguration {
	private static AppConfiguration appConf = null;
	private Configuration conf = null;

	public static Boolean hasInited() {
		if (appConf != null){
			return Boolean.valueOf(true);
		}
		return Boolean.valueOf(false);
	}

	public static AppConfiguration getInstance() {
		if (!hasInited().booleanValue()) {
			throw new NullPointerException("AppConfiguration has not been inited.");
		}
		return appConf;
	}
    
	//D:\local\tomcat7.0\webapps\crm\WEB-INF\conf\app-config.xml
	public static synchronized void init(String configFile) throws ConfigurationException {
		if (appConf == null) {
			appConf = new AppConfiguration();
			ConfigurationFactory factory = new ConfigurationFactory(configFile);
			appConf.conf = factory.getConfiguration();
			int pIndex = configFile.lastIndexOf("/");
			if (pIndex == -1)
				pIndex = configFile.lastIndexOf("\\");
			String basePath = configFile.substring(0, pIndex);
			//启动项目时获取数mysql数据库名
			Sysdata.getInstance().loadData();
            //启动项目将一些基础数据加入redis缓存中(xiaogq)
			Thread cacheThread = new Thread(new SystemCache("SystemCache"));
			cacheThread.start();
//			//开启获取阿里钉钉token信息线程
//			Thread ddTokenTimer = new Thread(new DdTokenTimer());
//			ddTokenTimer.start();
//			//开启获取阿里钉钉用户信息线程(会查询token信息)
//			Thread ddUserTimer = new Thread(new DdUserTimer());
//			ddUserTimer.start();

		}
	}

	public void clear() {
		if (this.conf != null) {
			this.conf.clear();
			this.conf = null;
		}
	}

}
